//
//  KATFocusWindow.m
//  KATFramework
//
//  Created by Kantice on 14/11/3.
//  Copyright (c) 2014年 KatApp. All rights reserved.
//

#import "KATFocusWindow.h"

@implementation KATFocusWindow

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


+ (instancetype)windowWithRect:(CGRect)rect cornerRadius:(float)radius andColor:(UIColor *)color andPadding:(float)padding
{
    
    KATFocusWindow *window=[[[self alloc] init] autorelease];
    
    window.color=color;
    window.isShown=NO;
    window.animateTime=0.6;
    window.padding=padding;
    
    window.screenSize=[[UIScreen mainScreen] bounds].size;//屏幕尺寸
    window.frame=CGRectMake(window.screenSize.width, 0, window.screenSize.width, window.screenSize.height);//开始隐藏
    window.backgroundColor=color;
    
    //蒙版
    CAShapeLayer *shape=[CAShapeLayer layer];
    shape.frame=window.bounds;
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    //外方
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(window.bounds.size.width, 0)];
    [path addLineToPoint:CGPointMake(window.bounds.size.width, window.bounds.size.height)];
    [path addLineToPoint:CGPointMake(0, window.bounds.size.height)];
    [path closePath];
    
    //内圆角矩形
    [path addArcWithCenter:CGPointMake(rect.origin.x+rect.size.width+padding-radius, rect.origin.y-padding+radius) radius:radius startAngle:M_PI*1.5 endAngle:M_PI*2 clockwise:YES];
    [path addLineToPoint:CGPointMake(rect.origin.x+rect.size.width+padding, rect.origin.y+rect.size.height+padding-radius)];
    [path addArcWithCenter:CGPointMake(rect.origin.x+rect.size.width+padding-radius, rect.origin.y+rect.size.height+padding-radius) radius:radius startAngle:M_PI*0 endAngle:M_PI*0.5 clockwise:YES];
    [path addLineToPoint:CGPointMake(rect.origin.x-padding+radius, rect.origin.y+rect.size.height+padding)];
    [path addArcWithCenter:CGPointMake(rect.origin.x-padding+radius, rect.origin.y+rect.size.height+padding-radius) radius:radius startAngle:M_PI*0.5 endAngle:M_PI*1 clockwise:YES];
    [path addLineToPoint:CGPointMake(rect.origin.x-padding, rect.origin.y-padding+radius)];
    [path addArcWithCenter:CGPointMake(rect.origin.x-padding+radius, rect.origin.y-padding+radius) radius:radius startAngle:M_PI*1 endAngle:M_PI*1.5 clockwise:YES];
    [path addLineToPoint:CGPointMake(rect.origin.x+rect.size.width+padding-radius, rect.origin.y-padding)];
    
    shape.path=path.CGPath;
    shape.fillRule=kCAFillRuleEvenOdd;
    shape.fillColor=[UIColor whiteColor].CGColor;
    
    window.layer.mask=shape;
    
    
    /*
    
    //黑幕（4块）
    UIView *b1=[[UIView alloc] init];
    b1.frame=CGRectMake(0, 0, window.frame.size.width, rect.origin.y-padding);//上
    b1.backgroundColor=color;
    [window addSubview:b1];
    [b1 release];
    
    UIView *b2=[[UIView alloc] init];
    b2.frame=CGRectMake(0, rect.origin.y+rect.size.height+padding, window.frame.size.width, window.frame.size.height-(rect.origin.y+rect.size.height+padding));//下
    b2.backgroundColor=color;
    [window addSubview:b2];
    [b2 release];
    
    UIView *b3=[[UIView alloc] init];
    b3.frame=CGRectMake(0, rect.origin.y-padding, rect.origin.x-padding, rect.size.height+2*padding);//左
    b3.backgroundColor=color;
    [window addSubview:b3];
    [b3 release];
    
    UIView *b4=[[UIView alloc] init];
    b4.frame=CGRectMake(rect.origin.x+rect.size.width+padding, rect.origin.y-padding, window.frame.size.width-(rect.origin.x+rect.size.width+padding), rect.size.height+2*padding);//右
    b4.backgroundColor=color;
    [window addSubview:b4];
    [b4 release];
    
    */
    
    
    UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc] initWithTarget:window action:@selector(hide)];//点击关闭
    singleTap.numberOfTapsRequired=1;//点击次数
    [window addGestureRecognizer:singleTap];//添加点击事件
    [singleTap release];
    singleTap=nil;
    
    
    return window;
}



//显示
- (void)show
{
    if(!_isShown)
    {
        self.frame=CGRectMake(0, 0, _screenSize.width, _screenSize.height);
        self.alpha=0.0;
        
        [UIView animateWithDuration:_animateTime animations:^
        {
            self.alpha=1.0;
        }
        completion:^(BOOL finished)
        {
            _isShown=YES;
        }];
    }
}


//隐藏
- (void)hide
{
    if(_isShown)
    {
        [UIView animateWithDuration:_animateTime animations:^
        {
             self.alpha=0.0;
        }
        completion:^(BOOL finished)
        {
            self.frame=CGRectMake(-_screenSize.width, 0, _screenSize.width, _screenSize.height);
             _isShown=NO;
        }];
    }
    
}




- (void)dealloc
{
    [_color release];
    
    [super dealloc];
}


@end
